﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Add_MaterialOrder : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        
        public frm_Add_MaterialOrder()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.orderDate.EditValue = DateTime.Now.Date;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void orderNo_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            this.orderNo.SelectAll();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DateTime order_date = new DateTime();
            DateTime dead_line = new DateTime();
            DateTime expired_date = new DateTime();

            string error = null;
            string order = null;

            if (string.IsNullOrEmpty(this.orderNo.Text))
            {
                error = "Please enter Order No";
            }
            else
            {
                order = this.orderNo.Text;
                if (order.Length != 6)
                {
                    error = "Invalid Order Number";
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.orderDate.EditValue != null)
                {
                    order_date = DateTime.Parse(this.orderDate.EditValue.ToString());
                }

                if (this.deadLine.EditValue != null)
                {
                    dead_line = DateTime.Parse(this.deadLine.EditValue.ToString());
                }
                /*
                if (this.expiredDate.EditValue != null)
                {
                    expired_date = DateTime.Parse(this.expiredDate.EditValue.ToString());
                }
                 */

                string invoice = this.invoiceValue.Text;
                string remark = this.remarkValue.Text;

                try
                {                    
                    myConnection.Open();
                    string sql = "INSERT INTO [444_MaterialOrder] (OrderNo, DeadLine, OrderDate, ExpiredDate, Invoice, Remark)"
                        + " VALUES ('" + order + "', @deadline, @orderdate, @expireddate, '" + invoice + "', '" + remark + "')";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@deadline", SqlDbType.DateTime).Value = dead_line.Date;
                    cmd_sql.Parameters.Add("@orderdate", SqlDbType.DateTime).Value = order_date.Date;
                    cmd_sql.Parameters.Add("@expireddate", SqlDbType.DateTime).Value = expired_date.Date;
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Material Orther added success.");
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Material Order. Please try again.");
                }
            }
        }
    }
}
